package com.econvent.common.conn;

import com.alibaba.druid.pool.DruidDataSource;
import com.econvent.common.conn.config.BaseConfig;
import com.econvent.common.conn.entity.BaseDataSourceProperty;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbcp2.BasicDataSource;
import com.mchange.v2.c3p0.*;

import javax.sql.DataSource;

/**
 * 实现多数据源的管理-根据配置文件或者数据库配置构建数据库连接池对象
 *
 * @author xiekun
 * @since 1.0
 */

@Slf4j
public class DynamicDataSourceDirector {
    private DataSourceBuilder dataSourceBuilder ;

    public DynamicDataSourceDirector(DataSourceBuilder dataSourceBuilder) {
        this.dataSourceBuilder = dataSourceBuilder;
    }

    /**
     * 构造数据源
     *
     * @param baseDataSourceProperty 数据源基础属性
     *
     * @return DataSource
     * **/
    public DataSource constructDataSource(BaseDataSourceProperty baseDataSourceProperty) {
        return this.constructDataSource(baseDataSourceProperty, baseDataSourceProperty.getBaseConfig()) ;
    }

    /**
     * 构造数据源
     *
     * @param baseDataSourceProperty 数据源基础属性
     * @param dataSourceConfig 不同数据库连接池特有属性
     *
     * @return DataSource
     * **/
    public DataSource constructDataSource(BaseDataSourceProperty baseDataSourceProperty, BaseConfig dataSourceConfig) {
        dataSourceBuilder.buildDataSource(baseDataSourceProperty);
        dataSourceBuilder.buildDataSourceConfig(dataSourceConfig);
        return dataSourceBuilder.build() ;
    }
}